﻿/****************
* Creat Date: 2020/08/04 03:55:26
* Creator: Quick.CodeGenerator
*****************/
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using BookStores.Entity;
using Dapper.Database;
using SqlSugar;
using System.Threading.Tasks;
using BookStores.DTO.Response;
using Utility.Extensions;

namespace BookStores.Repositories
{
    /// <summary>
    /// 数据表操作实体类：BookAuthors
    /// </summary>
    [Serializable]
    public partial class BookAuthorsRepository : QuickFramework.SqlSugarOrm.Repository<BookStores.Entity.BookAuthors>, BookStores.Contracts.IBookAuthorsRepository
    {
        /// <summary>
        /// 构造方法,初始化数据库链接：BookAuthors
        /// </summary>
        public BookAuthorsRepository() : base(QuickFramework.SqlSugarOrm.DbConnectionStrings.Default)
        { }
        /// <summary>
        /// 构造方法,初始化数据库链接：BookAuthors
        /// </summary>
        public BookAuthorsRepository(string dbConnectionString) : base(dbConnectionString)
        { }

        /// <summary>
        /// 获取作者列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<(List<BookAuthor>, int)> GetBookAuthors(string name, int pageIndex = 1, int pageSize = 10)
        {
            name = name.StrTrim();
            string[] kws = name?.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var queryable = base.Queryable<Entity.BookAuthors, Entity.Country>((authors, country) =>
                                                                        authors.Nationality == country.ID
                                                                    )
                                                                    .Select((authors, country) => new BookAuthor
                                                                    {
                                                                        ID = authors.ID,
                                                                        Author_Name = authors.Author_Name,
                                                                        Nationality = authors.Nationality,
                                                                        Nationality_CN = country.Name_CN,
                                                                        Nationality_EN = country.Name_EN
                                                                    })
                                                                    .WhereIF((null != kws && kws.Length > 0) && !string.IsNullOrEmpty(name), authors =>
                                                                                                  authors.Author_Name.Contains(name, StringComparison.OrdinalIgnoreCase)
                                                                                                  || kws.Contains(authors.ID)
                                                                                                  );

            var count = await queryable.CountAsync();
            var items = await queryable.ToPageListAsync(pageIndex, pageSize);
            return (items, count);
        }
        /// <summary>
        /// 获取作者列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<List<BookAuthor>> GetBookAuthorsByIds(IEnumerable<string> ids)
        {
            return await base.Queryable<Entity.BookAuthors, Entity.Country>((authors, country) =>
                                                             authors.Nationality == country.ID
                                                         ).Where(authors => ids.Contains(authors.ID))
                                                         .Select((authors, country) => new BookAuthor
                                                         {
                                                             ID = authors.ID,
                                                             Author_Name = authors.Author_Name,
                                                             Nationality = authors.Nationality,
                                                             Nationality_CN = country.Name_CN,
                                                             Nationality_EN = country.Name_EN
                                                         })
                                                         .ToListAsync();
        }
    }
}
